﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.


using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

using OOutlook = Microsoft.Office.Interop.Outlook;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    public partial class VDialerJournalOptionsForm : Form {
        private bool _isDirty = false;

        public VDialerJournalOptionsForm() {
            InitializeComponent();

            _cmbFolder.Items.AddRange(OutlookManager.GetJournalFolders());

            _cmbSubject.Items.AddRange(StringUtil.GetOptions(Properties.Resources.JOURNAL_OPTIONS_SUBJECT));
            _cmbSubject.SelectedIndex = 0;

            OOutlook.Categories categories = OutlookManager.Categories;
            StringUtil.Option[] catopts    = StringUtil.GetOptions(Properties.Resources.JOURNAL_OPTIONS_CATEGORY);

            foreach (StringUtil.Option catopt in catopts) {
                _cmbCategory.Items.Add(new CategoryItem(catopt.Id, catopt.Description));
            }

            foreach (OOutlook.Category category in categories){
                _cmbCategory.Items.Add(new CategoryItem(category.CategoryID, category.Name, category.CategoryBorderColor, category.CategoryGradientTopColor, category.CategoryGradientBottomColor));
            }

            VDialerJournalOptions options = VDialerConfiguration.Instance.JournalOptions;

            _chkUseDefaultFolder.Checked = options.UseDefaultFolder;
            _cmbFolder.SelectedItem      = options.Folder;
            _cmbSubject.SelectedItem     = new StringUtil.Option(options.Subject);
            _txtEntryType.Text           = options.EntryType;
            _cmbCategory.SelectedItem    = new CategoryItem(options.Category, null);
            _chkLinkContact.Checked      = options.LinkContact;
            _chkCopyCompany.Checked      = options.CopyCompany;
            _chkMarkPrivate.Checked      = options.MarkPrivate;
            _chkAutoSave.Checked         = options.AutoSave;
            _chkOpenForm.Checked         = options.OpenForm;
            
            _btnOk.Enabled = false;
            _isDirty       = false;
        }

        private void OnDrawItemCategory(object sender, DrawItemEventArgs e) {
            CategoryItem item = (CategoryItem)((e.Index > -1)?_cmbCategory.Items[e.Index]:null);
            
            e.DrawBackground();
            e.DrawFocusRectangle();

            if (item != null) {
                Rectangle textRect = e.Bounds;
                if (item.HasIcon) {
                    Rectangle iconRect = new Rectangle(textRect.X + 3, textRect.Y + 1, textRect.Height - 2, textRect.Height - 4);

                    using (GraphicsPath path = CreateRoundedRectanglePath(iconRect.X, iconRect.Y, iconRect.Width, iconRect.Height, 3)) {
                        using (Brush grad = new LinearGradientBrush(iconRect, item.TopGradientColor, item.BottomGradientColor, LinearGradientMode.Vertical)) {
                            e.Graphics.FillPath(grad, path);
                        }
                        using (Pen borderPen = new Pen(item.BorderColor, 1f)) {
                            e.Graphics.DrawPath(borderPen, path);
                        }
                    }

                    textRect.X += iconRect.Width + 4;
                    textRect.Width -= iconRect.Width + 4;

                }

                TextRenderer.DrawText(e.Graphics, item.Name, e.Font, textRect, e.ForeColor, e.BackColor, TextFormatFlags.VerticalCenter | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine);
            }
        }

        private void OnValueChanged(object sender, EventArgs e) {
            _cmbFolder.Enabled   = (!_chkUseDefaultFolder.Checked);
            _chkAutoSave.Enabled = (_chkOpenForm.Checked);
            _btnOk.Enabled       = true;
            _isDirty             = true;
        }

        private void OnButtonOk(object sender, EventArgs e) {
            if (_isDirty) {
                VDialerJournalOptions options = VDialerConfiguration.Instance.JournalOptions;

                options.UseDefaultFolder = _chkUseDefaultFolder.Checked;
                options.Folder           = (_cmbFolder.SelectedItem != null) ? ((OutlookFolderRef)_cmbFolder.SelectedItem) : null;
                options.Subject          = (_cmbSubject.SelectedItem != null) ? ((StringUtil.Option)_cmbSubject.SelectedItem).Id : null;
                options.EntryType        = _txtEntryType.Text;
                options.Category         = (_cmbCategory.SelectedItem != null) ? ((CategoryItem)_cmbCategory.SelectedItem).Id : null;
                options.LinkContact      = _chkLinkContact.Checked;
                options.CopyCompany      = _chkCopyCompany.Checked;
                options.MarkPrivate      = _chkMarkPrivate.Checked;
                options.AutoSave         = _chkAutoSave.Checked;
                options.OpenForm         = _chkOpenForm.Checked;

                options.Save();
            }
        }

        private static GraphicsPath CreateRoundedRectanglePath(int x, int y, int width, int height, int radius) {
            GraphicsPath p = new GraphicsPath();
            p.StartFigure();

            p.AddArc(x, y, 2 * radius, 2 * radius, 180, 90);
            p.AddLine(x + radius, y, x + width - radius, y);
            p.AddArc(x + width - 2 * radius, y, 2 * radius, 2 * radius, 270, 90);
            p.AddLine(x + width, y + radius, x + width, y + height - radius);
            p.AddArc(x + width - 2 * radius, y + height - 2 * radius, 2 * radius, 2 * radius, 0, 90);
            p.AddLine(x + width - radius, y + height, x + radius, y + height);
            p.AddArc(x, y + height - 2 * radius, 2 * radius, 2 * radius, 90, 90);
            p.AddLine(x, y + height - radius, x, y + radius);

            p.CloseFigure();
            return p;
        }

        internal class CategoryItem {
            private string _id;
            private string _name;
            private bool _icon = false;
            private Color _borderColor;
            private Color _topColor;
            private Color _bottomColor;

            public CategoryItem(string id, string name) {
                _id = id;
                _name = name;
            }

            public CategoryItem(string id, string name, uint borderColor, uint gradientTop, uint gradientBottom)
                : this(id, name) {
                _borderColor = ColorTranslator.FromOle((int)borderColor);
                _topColor = ColorTranslator.FromOle((int)gradientTop);
                _bottomColor = ColorTranslator.FromOle((int)gradientBottom);
                _icon = true;

            }

            public string Id { get { return _id; } }
            public string Name { get { return _name; } }
            public bool HasIcon { get { return _icon; } }
            public Color BorderColor { get { return _borderColor; } }
            public Color TopGradientColor { get { return _topColor; } }
            public Color BottomGradientColor { get { return _bottomColor; } }

            public override bool Equals(object obj) {
                if (obj == null)
                    return false;

                CategoryItem p = obj as CategoryItem;
                if ((System.Object)p == null)
                    return false;

                return (_id.Equals(p._id));
            }

            public override int GetHashCode() {
                return _id.GetHashCode();
            }


            public override string ToString() {
                return _name;
            }
        }

    }
}
